package com.atwork.wuhua.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.SubLaunchAdapter;
import com.atwork.wuhua.adapter.TypesAdapter;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.bean.HomePageBean;
import com.atwork.wuhua.bean.TypesBean;
import com.atwork.wuhua.mvp.presenter.SubLaunchPresenter;
import com.atwork.wuhua.mvp.view.ISubLaunchActivity;
import com.atwork.wuhua.utils.TypeSelectUtil;
import com.atwork.wuhua.utils.Util;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 发起约战
 */
public class SubLaunchActivity extends BaseActivity implements ISubLaunchActivity {
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_sub_launch)
    RecyclerView rvSubLaunch;
    @BindView(R.id.ed_search)
    EditText edSearch;
    @BindView(R.id.tv_region)
    TextView tvRegion;
    @BindView(R.id.lly_region)
    LinearLayout llyRegion;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.lly_level)
    LinearLayout llyLevel;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.lly_distance)
    LinearLayout llyDistance;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.img_region_type)
    ImageView imgRegionType;
    @BindView(R.id.img_distance_top)
    ImageView imgDistanceTop;
    @BindView(R.id.img_distance_boom)
    ImageView imgDistanceBoom;
    @BindView(R.id.rv_types)
    RecyclerView rvTypes;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;
    @BindView(R.id.img_level_top)
    ImageView imgLevelTop;
    @BindView(R.id.img_level_boom)
    ImageView imgLevelBoom;
    @BindView(R.id.lly_no_date)
    LinearLayout llyNoDate;

    private SubLaunchPresenter presenter;
    private SubLaunchAdapter subLaunchAdapter;
    private TypesAdapter typesAdapter;

    private int action = MOVE_UPDATA;
    private int total;
    private int timeCode = 1; // 时间排序 1 离最近的时间 2 最远的时间
    private String mtype = ""; //竞技类型
    private String keywords = "";//搜索关键词 非必须参数
    private String area = "0";//区域
    private String star = "0";//星级
    private String sort = "ASC";
    private String orders = "";
    private List<TypesBean.DataBean> typesBeans;
    private List<HomePageBean.DataBean> subLaunchBeans;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_sub_launch;
    }

    @Override
    public void initView() {
        tvTitle.setText("发起约战");
        presenter = new SubLaunchPresenter();
        presenter.attachView(this);
        presenter.getTypes();

        subLaunchBeans = new ArrayList<>();
        typesBeans = new ArrayList<>();
    }

    @Override
    public void initData() {
        LinearLayoutManager typemanger = new LinearLayoutManager(this);
        typemanger.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvTypes.setLayoutManager(typemanger);
        typesAdapter = new TypesAdapter(this, R.layout.item_types, typesBeans);
        rvTypes.setAdapter(typesAdapter);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvSubLaunch.setLayoutManager(manager);

        subLaunchAdapter = new SubLaunchAdapter(this, R.layout.item_sub_launch, subLaunchBeans);
        rvSubLaunch.setAdapter(subLaunchAdapter);

        RefreshData();

        subLaunchAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(SubLaunchActivity.this, SubLaunDetailActivity.class);
                intent.putExtra(ConstantsMyself.INTENTID, subLaunchBeans.get(position).getId());
                startActivity(intent);
            }
        });

        typesAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.lly_click:
                        for (int i = 0; i < typesBeans.size(); i++) {
                            if (i == position) {
                                typesBeans.get(i).setSelect(true);
                                mtype = typesBeans.get(i).getId();
                            } else {
                                typesBeans.get(i).setSelect(false);
                            }
                        }
                        typesAdapter.setNewData(typesBeans);
                        action = MOVE_UPDATA;
                        presenter.getLists(mtype, 1, keywords, area, orders, sort);
                        break;
                }
            }
        });

        initScreen();
    }

    /**
     * h筛选
     */
    private void initScreen() {
        TypeSelectUtil typeSelectUtil = new TypeSelectUtil(this, llyRegion, imgRegionType, tvRegion,
                llyLevel, imgLevelTop, imgLevelBoom, llyDistance, imgDistanceTop, imgDistanceBoom);
        typeSelectUtil.setOnSelectResult(new TypeSelectUtil.OnSelectResultLinstener() {
            @Override
            public void onselect(int type, String code) {
                switch (type) {
                    case TypeSelectUtil.REGIN:
                        area = code;
                        action = MOVE_UPDATA;
                        presenter.getLists(mtype, 1, keywords, area, orders, sort);
                        break;
                    case TypeSelectUtil.LEVEL:
                        sort = code;
                        action = MOVE_UPDATA;
                        orders = "star";
                        presenter.getLists(mtype, 1, keywords, area, orders, sort);

                        break;
                    case TypeSelectUtil.DISTANCE:
                        sort = code;
                        action = MOVE_UPDATA;
                        orders = "discount";
                        presenter.getLists(mtype, 1, keywords, area, orders, sort);
                        break;

                }
            }
        });

        edSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                keywords = s.toString().trim();
                if (s.toString().length() == 0) {
                    keywords = "";
                    presenter.getLists(mtype, 1, keywords, area, orders, sort);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void setTypesData(TypesBean typesBean) {
        typesBeans.clear();
        typesBeans.addAll(typesBean.getData());
        typesBeans.get(0).setSelect(true);
        mtype = typesBeans.get(0).getId();
        typesAdapter.setNewData(typesBeans);
        presenter.getLists(mtype, 1, keywords, area, star, sort);
    }

    @Override
    public void setLists(HomePageBean data) {
        if (action == MOVE_UPDATA) {
            subLaunchBeans.clear();
        }
        total = data.getMeta().getTotal();
        subLaunchBeans.addAll(data.getData());
        subLaunchAdapter.setNewData(subLaunchBeans);
        swipe.setRefreshing(false);
        swipe.setVisibility(View.VISIBLE);
        llyNoDate.setVisibility(View.GONE);
    }

    @Override
    public void noData() {
        total = 0;
        subLaunchBeans.clear();
        subLaunchAdapter.setNewData(subLaunchBeans);
        swipe.setRefreshing(false);

        swipe.setVisibility(View.GONE);
        llyNoDate.setVisibility(View.VISIBLE);
    }

    private void RefreshData() {
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                action = MOVE_UPDATA;
                presenter.getLists(mtype, 1, keywords, area, star, sort);
            }
        });

        subLaunchAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (subLaunchBeans.size() < total) {
                    action = MOVE_LOADING;
                    presenter.getLists(mtype, subLaunchBeans.size() / 10 + 1, keywords, area, star, sort);
                } else {
                    subLaunchAdapter.loadMoreEnd();//数据加载完毕
                }

            }
        }, rvSubLaunch);
    }

    @OnClick({R.id.img_left, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_left:
                finish();
                break;

            case R.id.tv_search:
                keywords = edSearch.getText().toString().trim();
                action = MOVE_UPDATA;
                presenter.getLists(mtype, 1, keywords, area, star, sort);
                Util.hideSoftInputFromWindow(this);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
